package xin.marcher.module.seller.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xin.marcher.module.common.exception.BaseBizException;
import xin.marcher.module.seller.domain.dto.SellerAccountDTO;
import xin.marcher.module.seller.domain.request.SellerAccountRequest;
import xin.marcher.module.seller.repository.SellerRepository;
import xin.marcher.module.seller.service.SellerAccountService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 *
 */
@Service
public class SellerAccountServiceImpl implements SellerAccountService {

    @Resource
    private SellerRepository sellerRepository;

    /**
     * 保存一个用户账号信息
     *
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveSellerAccountInfo(SellerAccountRequest request) {
        // 校验参数是否为null
        checkAccountParam(request);

        return sellerRepository.saveSellerAccountInfo(request);
    }

    /**
     * 校验参数
     *
     * @param request
     */
    private void checkAccountParam(SellerAccountRequest request) {
        if (Objects.isNull(request)
                || Objects.isNull(request.getAccountNo())
                || Objects.isNull(request.getSellerId())
                || Objects.isNull(request.getPayChannel())) {
            throw new BaseBizException("参数不能为null");
        }
    }

    /**
     * 更新支付渠道信息
     *
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateSellerAccountInfo(SellerAccountRequest request) {
        // 校验参数是否为null
        checkAccountParam(request);
        return sellerRepository.updateSellerAccountInfo(request);
    }

    /**
     * 根据卖家id查询卖家全部的账号信息
     *
     * @param sellerId
     * @return
     */
    @Override
    public List<SellerAccountDTO> queryAccountsBySellerId(Long sellerId) {
        return sellerRepository.queryAccountsBySellerId(sellerId);
    }

    /**
     * 删除一个卖家账号
     *
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteAccount(SellerAccountRequest request) {
        // 校验参数是否为null
        checkAccountParam(request);
        return sellerRepository.deleteAccount(request);
    }
}